package com.faintlight.config;

import com.faintlight.service.CapsuleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@Configuration
@EnableScheduling
public class SchedulerConfig {

    private static final Logger logger = LoggerFactory.getLogger(SchedulerConfig.class);

    private final CapsuleService capsuleService;

    public SchedulerConfig(CapsuleService capsuleService) {
        this.capsuleService = capsuleService;
        logger.info("✅ 定时任务配置已加载");
    }

    @Scheduled(cron = "0 0 * * * ?")
    public void checkCapsuleUnlock() {
        logger.info("⏰ 开始执行每日胶囊解锁任务...");
        try {
            capsuleService.processUnlockTasks();
            logger.info("✅ 时光胶囊解锁任务执行完成");
        } catch (Exception e) {
            logger.error("❌ 时光胶囊解锁任务失败", e);
        }
    }
}
